package site.weiyikai.springbootipstarter.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import site.weiyikai.springbootipstarter.properties.IpProperties;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * @author 程序员小魏
 * @create 2022-12-11 15:36
 */
public class IpCountService {

    @Autowired
    private IpProperties ipProperties;

    private Map<String, Integer> ipCountMap = new HashMap<String, Integer>();

    @Autowired
    //当前的request对象的注入工作由使用当前starter的工程提供自动装配
    private HttpServletRequest httpServletRequest;

    public void count() {
        String ip = httpServletRequest.getRemoteAddr();
        //2.根据IP地址从Map取值，并递增
        Integer count = ipCountMap.get(ip);
//        String num = stringRedisTemplate.opsForValue().get(ip);
//        if(num == null){
//            stringRedisTemplate.opsForValue().set(ip,"1");
//        }else{
//            stringRedisTemplate.opsForValue().set(ip,Integer.parseInt(num) + 1 + "");
//        }
        if (count == null) {
            ipCountMap.put(ip, 1);
        } else {
            ipCountMap.put(ip, count + 1);
        }
        if (ipProperties.getModel().equals(IpProperties.LogModel.DETAIL.getValue())) {
            System.out.println("         IP访问监控");
            System.out.println("+-----ip-address-----+--num--+");
            for (Map.Entry<String, Integer> entry : ipCountMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(String.format("|%18s  |%5d  |", key, value));
//            Set<String> keys = stringRedisTemplate.keys("*");
//            if(keys != null && keys.size() > 0) {
//                for (String key : keys) {
//                    Integer value = Integer.parseInt(stringRedisTemplate.opsForValue().get(key));
//                    System.out.println(String.format("|%18s  |%5d  |",key,value));
//                }
//            }else{
//                throw new RuntimeException("服务器出现异常!");
//            }
                System.out.println("+--------------------+-------+");
            }
        } else {
            System.out.println("         IP访问监控");
            System.out.println("+-----ip-address-----+");
            for (Map.Entry<String, Integer> entry : ipCountMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(String.format("|%18s  |", key));
            }
//            Set<String> keys = stringRedisTemplate.keys("*");
//            if(keys != null && keys.size() > 0) {
//                for (String key : keys) {
//                    //*Integer value = Integer.parseInt(stringRedisTemplate.opsForValue().get(key));*//*
//                    System.out.println(String.format("|%18s  |",key));
//                }
//            }else{
//                throw new RuntimeException("服务器出现异常!");
//            }
            System.out.println("+--------------------+");
        }
        ;

    }

    @Scheduled(cron = "0/#{ipProperties.cycle} * * * * ?")
    public void print() {
        if (ipProperties.getModel().equals(IpProperties.LogModel.DETAIL.getValue())) {
            System.out.println("         IP访问监控");
            System.out.println("+-----ip-address-----+--num--+");
            for (Map.Entry<String, Integer> entry : ipCountMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(String.format("|%18s  |%5d  |", key, value));
            }
            /*Set<String> keys = stringRedisTemplate.keys("*");
            if(keys != null && keys.size() > 0) {
                for (String key : keys) {
                    Integer value = Integer.parseInt(stringRedisTemplate.opsForValue().get(key));
                    System.out.println(String.format("|%18s  |%5d  |",key,value));
                }
            }else{
                throw new RuntimeException("服务器出现异常!");
            }*/

            System.out.println("+--------------------+-------+");
        } else {
            System.out.println("         IP访问监控");
            System.out.println("+-----ip-address-----+");
            for (Map.Entry<String, Integer> entry : ipCountMap.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(String.format("|%18s  |", key));
            }
//            Set<String> keys = stringRedisTemplate.keys("*");
//            if(keys != null && keys.size() > 0) {
//                for (String key : keys) {
//                    *//*Integer value = Integer.parseInt(stringRedisTemplate.opsForValue().get(key));*//*
//                    System.out.println(String.format("|%18s  |",key));
//                }
//            }else{
//                throw new RuntimeException("服务器出现异常!");
//            }
//            System.out.println("+--------------------+");
//        };
            if (ipProperties.getCycleReset()) {
                ipCountMap.clear();
            }
        }
    }
}
